package business_logic.game_elements;


import java.util.Collection;
import java.util.EnumSet;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import business_logic.GameElement;
import business_logic.Tile;
import business_logic.view.GameViewIdentifier;


import model.collisions.CollisionCategory;
import model.elements.Bullet;
import model.elements.attributes.Coordinate2D;
import model.elements.attributes.Damage;
import model.elements.states.movement.ElementMovementState;
import model.exceptions.ModelExceptions;

public final class Proyectil extends Bullet implements GameElement
{
	
	private static final CollisionCategory 	ProyectilBlastType 	= CollisionCategory.C_L1_EXPLOTION;
	private static final Damage 			ProyectilDamage		= new Damage(5);
	private static final double 			ProyectilSpeed		= 0.1;
	private static final int				ProyectilRange		= 10*Tile.speedToTickets( ProyectilSpeed );
	private static final int				ProyectilBlastRange	= 1;
	
    private static final Collection<GameViewIdentifier> m_viewIdentifiers = 
            EnumSet.of(GameViewIdentifier.SPRITE_PROYECTIL);
    
    public Proyectil(Coordinate2D position, ElementMovementState direction) throws ModelExceptions
    {
        super(	
        	position, 
        	direction,
        	ProyectilBlastType, 
        	ProyectilSpeed, 
        	ProyectilDamage, 
        	ProyectilBlastRange
        );
		super.setTTL( ProyectilRange );
    }

    public Collection<GameViewIdentifier> getViewIdentifiers()
    {
        return m_viewIdentifiers;
    }

    @Override
    public void vivir()
    {
        try
        {
            this.update();
        }
        catch (ModelExceptions e)
        {
        }
    }

    @Override
    public int getX()
    {
        return Tile.pixelToTile( m_currentPosition.getRealX() );
    }

    @Override
    public int getY()
    {
        return Tile.pixelToTile( m_currentPosition.getRealY() );
    }

    private static final long m_serialVersionUID = 1L;
    
    @Override
    public Element save()
    {
        Element elem = DocumentHelper.createElement( this.getClass().getName() );
        elem.addAttribute( "serialVersion", m_serialVersionUID+"" );
        elem.addAttribute( "x", Math.floor( this.getPosition().getRealX() ) +"" );
        elem.addAttribute( "y", Math.floor( this.getPosition().getRealY() ) +"" );
        elem.addAttribute( "direction", this.getDirection().getClass().getName()+"" );
        elem.addAttribute( "ttl", this.m_ttl.getValue()+"" );
        
        return elem;
    }
    
    public Proyectil( Element elem ) throws ModelExceptions, NumberFormatException, InstantiationException, IllegalAccessException, ClassNotFoundException
    {
        super
        (
            new Coordinate2D
            (
                Double.parseDouble( elem.attributeValue("x") ),
                Double.parseDouble( elem.attributeValue("y") )
            ),
            (ElementMovementState) Class.forName( elem.attributeValue("direction") ).newInstance(),
            ProyectilBlastType,
            ProyectilSpeed,
            ProyectilDamage,
            ProyectilBlastRange
        );
        
        super.setTTL( Integer.parseInt( elem.attributeValue("ttl") ) );
    }
    
}